/*
 * TransferQueue.java
 *
 * Created on December 3, 2006, 8:09 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package eu.somatik.somaftp.logic;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author francisdb
 */
public class TransferQueue extends AbstractTableModel{
    
    private static final String[] COL_NAMES = {"source", "destination"};
    
    private List<TransferQueueEntry> queue;
    
    /** Creates a new instance of TransferQueue */
    public TransferQueue() {
        queue = Collections.synchronizedList(new LinkedList<TransferQueueEntry>());
    }
    
    
    
    public void add(TransferQueueEntry...entrys){
        for(TransferQueueEntry entry:entrys){
            System.out.println("Adding to queue: "+entry);
        }
        queue.addAll(Arrays.asList(entrys));
        fireTableRowsInserted(queue.size() - entrys.length, entrys.length-1);
    }
    
    public TransferQueueEntry getNext(){
        TransferQueueEntry entry = null;
        TransferQueueEntry curEntry;
        for(int i = 0; i < queue.size() && entry == null;i++){
           curEntry = queue.get(i);
           if(curEntry.getStatus() == TransferStatus.QUEUED){
               entry = curEntry;
           }
        }
        return entry;
    }
    
    public void remove(TransferQueueEntry entry){
        int index = queue.indexOf(entry);
        queue.remove(index);
        fireTableRowsDeleted(index,index);
    }
    
    public void clear(){
        if(queue.size() > 0){
            int size = queue.size();
            queue.clear();
            fireTableRowsDeleted(0, size - 1);
        }
    }

    public int getRowCount() {
        return queue.size();
    }

    public int getColumnCount() {
        return COL_NAMES.length;
    }
    
    public String getColumnName(int columnIndex){
        return COL_NAMES [columnIndex];
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Object value = null;
        switch(columnIndex){
            case 0:
                value = queue.get(rowIndex).getFromPath();
                break;
            case 1:
                value = queue.get(rowIndex).getToPath();
                break;
            default:
                assert false : "should never come here";
        }
        return value;
    }
    
}
